import yaml
import os
import blue
TEMPLATETYPES = {'INVALID': 0,
 'BUILDING': 1,
 'AVATAR': 2,
 'PLACEABLE': 3}
latestVersion = 2

class PaletteTemplate(object):

    def __init__(self):
        self.version = latestVersion
        self.yamlRes = ''
        self.blueRes = ''
        self.classType = ''
        self.iconS = ''
        self.iconL = ''
        self.alignGround = False
        self.description = ''
        self.configData = {}
        self.minBounds = (0.0, 0.0, 0.0)
        self.maxBounds = (0.0, 0.0, 0.0)
        self.RestoreTransientData(('',
         '',
         '',
         TEMPLATETYPES['INVALID']))



    def UpgradeToVersion1_0(data):
        data.version = 1
        data.minBounds = (0.0, 0.0, 0.0)
        data.maxBounds = (0.0, 0.0, 0.0)
        return data


    UpgradeToVersion1_0 = staticmethod(UpgradeToVersion1_0)

    def UpgradeToVersion2_1(data):
        import actorfactory.PaletteTemplate as newTemp
        newData = newTemp.PaletteTemplate()
        newData.version = 2
        newData.yamlRes = data.yamlRes
        newData.blueRes = data.blueRes
        newData.classType = data.classType
        newData.iconS = data.iconS
        newData.iconL = data.iconL
        newData.alignGround = data.alignGround
        newData.description = data.description
        newData.configData = data.configData
        newData.minBounds = data.minBounds
        newData.maxBounds = data.maxBounds
        print ('Import Warning: The AT file for %s (%s) is being upgraded to Version 2' % (newData.blueRes, newData.yamlRes))
        newData.resave = True
        return newData


    UpgradeToVersion2_1 = staticmethod(UpgradeToVersion2_1)

    def VersionUpdate(data):
        if (hasattr(data, 'version') == False):
            data = PaletteTemplate.UpgradeToVersion1_0(data)
        if (data.version == 1):
            data = PaletteTemplate.UpgradeToVersion2_1(data)
        return data


    VersionUpdate = staticmethod(VersionUpdate)

    def Load(path):
        filePath = path
        if (path[:4].upper() == 'RES:'):
            filePath = blue.rot.PathToFilename(path)
        if (os.path.exists(filePath) == False):
            return 
        file = open(filePath)
        if (file != None):
            data = yaml.load(file)
            file.close()
            data = PaletteTemplate.VersionUpdate(data)
            data.CreateTransisntData(path)
            if hasattr(data, 'resave'):
                del data.resave
                print 'Resaving ',
                print (((data.path + '/') + data.fileName) + data.extension)
                data.Save((((data.path + '/') + data.fileName) + data.extension))
            return data


    Load = staticmethod(Load)

    def Save(self, path):
        filePath = path
        if (path[:4].upper() == 'RES:'):
            filePath = blue.rot.PathToFilename(path)
        if (os.path.exists(filePath) == False):
            return 
        saveData = self.RemoveTransientData()
        try:
            saveFile = file(filePath, 'w')
            yaml.dump(self, saveFile, default_flow_style=False)
            saveFile.close()
        except:
            print 'Failed to save file ',
            print filePath
        self.RestoreTransientData(saveData)



    def RemoveTransientData(self):
        saveData = (self.path,
         self.fileName,
         self.extension,
         self.classID)
        del self.path
        del self.fileName
        del self.extension
        del self.classID
        return saveData



    def RestoreTransientData(self, saveData):
        self.path = saveData[0]
        self.fileName = saveData[1]
        self.extension = saveData[2]
        self.classID = saveData[3]



    def CreateTransisntData(self, path):
        (self.path, self.fileName,) = os.path.split(path)
        (self.fileName, self.extension,) = os.path.splitext(self.fileName)
        if (self.classType in TEMPLATETYPES):
            self.classID = TEMPLATETYPES[self.classType]
        else:
            self.classID = TEMPLATETYPES['INVALID']



    def __repr__(self):
        if (self.yamlRes == ''):
            return ((((self.classType + ' Blue: ') + self.blueRes) + ' : ') + self.description)
        else:
            return ((((self.classType + ' Yaml: ') + self.yamlRes) + ' : ') + self.description)



    def GetIsYaml(self):
        return (self.yamlRes != '')



    def GetRes(self):
        if self.GetIsYaml():
            if (self.yamlRes[:4].upper() == 'RES:'):
                return blue.rot.PathToFilename(self.yamlRes)
            else:
                return ((self.path + '\\') + self.yamlRes)
        else:
            if (self.blueRes[:4].upper() == 'RES:'):
                return blue.rot.PathToFilename(self.blueRes)
            else:
                return ((self.path + '\\') + self.blueRes)



    def GetIconS(self):
        if (self.iconS[:4].upper() == 'RES:'):
            return blue.rot.PathToFilename(self.iconS)
        else:
            return ((self.path + '\\') + self.iconS)



    def GetIconL(self):
        if (self.iconL[:4].upper() == 'RES:'):
            return blue.rot.PathToFilename(self.iconL)
        else:
            return ((self.path + '\\') + self.iconL)



    def GetClassType(self):
        return self.classType



    def GetClassID(self):
        return self.classID



    def GetAlignGround(self):
        return self.alignGround



    def GetDescription(self):
        return self.description



    def GetName(self):
        return self.fileName



    def GetFileName(self):
        return ((self.fileName + '.') + self.extension)



    def GetFullPath(self):
        path = (((self.path + '/') + self.fileName) + self.extension)
        if (path[:4].upper != 'RES:'):
            return blue.rot.FilenameToPath(path)
        else:
            return path




